/////////////////////////////////////////////////////////////////////////////
// Name:        wxmudcommandline.cpp
// Purpose:     
// Author:      Stephen Whittle
// Modified by: 
// Created:     14/09/2009 18:21:30
// RCS-ID:      
// Copyright:   
// Licence:     
/////////////////////////////////////////////////////////////////////////////

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

////@begin includes
////@end includes

#include "wxmudcommandline.h"
#include "globals.h"
////@begin XPM images
////@end XPM images


/*
 * wxMudCommandLine type definition
 */

IMPLEMENT_DYNAMIC_CLASS( wxMudCommandLine, wxTextCtrl )


/*
 * wxMudCommandLine event table definition
 */

BEGIN_EVENT_TABLE( wxMudCommandLine, wxTextCtrl )

////@begin wxMudCommandLine event table entries
    EVT_TEXT_ENTER( ID_CMDBARMAINCTRL, wxMudCommandLine::OnTextEnter )
    EVT_KEY_DOWN( wxMudCommandLine::OnInputKeyDown )

////@end wxMudCommandLine event table entries

END_EVENT_TABLE()


/*
 * wxMudCommandLine constructors
 */

wxMudCommandLine::wxMudCommandLine()
{
    Init();
}

wxMudCommandLine::wxMudCommandLine(wxWindow* parent, wxWindowID id, const wxString& value, const wxPoint& pos, const wxSize& size, long style, const wxValidator& validator)
{
    Init();
    Create(parent, id, value, pos, size, style, validator);
}


/*
 * wxMudCommandLine creator
 */

bool wxMudCommandLine::Create(wxWindow* parent, wxWindowID id, const wxString& value, const wxPoint& pos, const wxSize& size, long style, const wxValidator& validator)
{
////@begin wxMudCommandLine creation
    wxTextCtrl::Create(parent, id, value, pos, size, style, validator);
    CreateControls();
////@end wxMudCommandLine creation
    this->Connect(wxID_ANY, wxEVT_KEY_DOWN, wxKeyEventHandler(wxMudCommandLine::OnInputKeyDown), NULL, this);
    mHistoryItems.push_back(value);
    mHistoryCurrent = 0;
  
  return true;
}


/*
 * wxMudCommandLine destructor
 */

wxMudCommandLine::~wxMudCommandLine()
{
////@begin wxMudCommandLine destruction
////@end wxMudCommandLine destruction
}


/*
 * Member initialisation
 */

void wxMudCommandLine::Init()
{
////@begin wxMudCommandLine member initialisation
////@end wxMudCommandLine member initialisation
}

 
/*
 * Control creation for wxMudCommandLine
 */

void wxMudCommandLine::CreateControls()
{    
////@begin wxMudCommandLine content construction
    this->SetFont(wxFont(10, wxSWISS, wxNORMAL, wxNORMAL, false, wxT("")));
////@end wxMudCommandLine content construction
}




/*
 * wxEVT_KEY_DOWN event handler for ID_TEXTCTRL
 */

void wxMudCommandLine::OnInputKeyDown( wxKeyEvent& event )
{
    int code = event.GetKeyCode();
    if (code == WXK_UP)
        HistPrev(); 
    else if (code == WXK_DOWN)
        HistNext();
    else
        event.Skip();
}

void wxMudCommandLine::OnTextEnter( wxCommandEvent& event )
{

    
        if (mudConnected){
            //check for string separator and for speedwalk too
            wxString input = commandLine->GetValue();
            wxString input2 = commandLine->GetValue() + wxT("\r\n");
            input2.Replace(pConfig->Read(wxT("/Terminal/CommandSeperator"),wxT(";")),wxT("\r\n"));
            telnet_send(&myTelnet_t,input2.mb_str(),input2.Length() ); 
            //wxAssert(mHistoryItems.size() > 0);

            // Save the item in our command history.
            if (input != wxEmptyString){
                mHistoryItems[mHistoryItems.size() - 1] = input;

                // Prepare next item in command history
                mHistoryItems.push_back(wxString(wxT("")));
                mHistoryCurrent = mHistoryItems.size() - 1;

                bool doEcho;
                pConfig->Read(wxT("/Terminal/LocalEcho"), &doEcho, true); 
                if (doEcho){
                    displayOutput(input2);
                }
                commandLine->Clear();
            }
            //event.Skip();
        }
//need to check if we are connected before doing this
    
}



// History manipulation functions.
void wxMudCommandLine::SaveCurrHist() {
    mHistoryItems[mHistoryCurrent] = GetValue();
}

void wxMudCommandLine::DisplayCurrHist() {
    SetValue(mHistoryItems[mHistoryCurrent]);
    SetInsertionPointEnd();
}

void wxMudCommandLine::HistPrev() {
    if (mHistoryCurrent > 0) {
        // Note: This uses Bash's semantics, which are to save the current
        // value of the history item you are editing when you go to another
        // line. The probably more correct behavior would be to have two 
        // copies of the history vector, one which was the actual history,
        // and the other of which was your current working copy. When you 
        // hit enter, your current working copy would be replaced with the
        // historical copy. Feel free to implement this if it floats your
        // boat.
        SaveCurrHist();
        mHistoryCurrent--;
        DisplayCurrHist();
    }
}

void wxMudCommandLine::HistNext() {
    if (mHistoryCurrent+1 < mHistoryItems.size()) {
        SaveCurrHist();
        mHistoryCurrent++;
        DisplayCurrHist();
    }
    //need to fix things here
}
